/**
 * @author jaanush
 */
dojo.provide("slipstream.data.JsonQueryRestStoreTrans");
dojo.require("slipstream.data.JsonRestStoreTrans");

dojo.declare("slipstream.data.JsonQueryRestStoreTrans", [slipstream.data.JsonRestStoreTrans], {

	oldgetValue: function(/*Object*/ item, /*String*/property, /*value?*/defaultValue){
		if (property.indexOf('{lang}') === 0) {
			//console.log(property,'Lang key');
			property = property.substring(6, property.length);
			if(item['Translation'] && item['Translation'][this.lang]) var value = item['Translation'][this.lang][property];
		} else {
			var value = item[property];
		}
		//console.log(item,property,'Fetching');
			//console.log(property,'data for');
		return value || // return the plain value since it was found;
					/*item['Translation'][this.lang][property] ||  *///Checkning for the value in the Translations array
			(property in item ? // a truthy value was not found, see if we actually have it 
				value : // we do, so we can return it
					(item['Translation'] && item['Translation'][this.lang] && (property in item['Translation'][this.lang]) ?
							item['Translation'][this.lang][property] :
								(item._loadObject ? // property was not found, maybe because the item is not loaded, we will try to load it synchronously so we can get the property 
									(dojox.rpc._sync = true) && arguments.callee.call(this,dojox.data.ServiceStore.prototype.loadItem({item:item}) || {}, property, defaultValue) : // load the item and run getValue again 
								defaultValue)
					 )); // not in item -> return default value
	},

	oldhasAttribute: function(item,attribute){
		if (attribute.indexOf('{lang}') === 0) var attribute = attribute.substring(6, attribute.length);
		return (attribute in item || (item['Translation'] && item['Translation'][this.lang] && (attribute in item['Translation'][this.lang])));		
	},

	oldsetValue: function(item, attribute, value){
		console.log('Setting attribute '+attribute+' to:',value);
		// summary:
		//		sets 'attribute' on 'item' to 'value'
		if (attribute.indexOf('{lang}') === 0) {
			console.log('Found forced lang attribute '+attribute);
			var attribute = attribute.substring(6, attribute.length);
			var forceLang=true;
		}
		var old = item[attribute] || (item['Translation'] && item['Translation'][this.lang] && (attribute in item['Translation'][this.lang]) ? item['Translation'][this.lang][attribute] : undefined);
		console.log('Old value',old);
		var store = item.__id ? dojox.data._getStoreForItem(item) : this;
		if(dojox.json.schema && store.schema && store.schema.properties){
			// if we have a schema and schema validator available we will validate the property change
			dojox.json.schema.mustBeValid(dojox.json.schema.checkPropertyChange(value,store.schema.properties[attribute]));
		}
		if(attribute == store.idAttribute){
			throw new Error("Can not change the identity attribute for an item");
		}
		if(forceLang || (item['Translation']!=undefined && item.Translation[this.lang]!=undefined && item.Translation[this.lang][attribute])){
			console.log(value,'Setting translated attribute '+attribute+' to ');
			if(item['Translation']==undefined)item['Translation']={};
			if(item['Translation'][this.lang]==undefined)item['Translation'][this.lang]={};
			store.changing(item);
			item['Translation'][this.lang][attribute]=value;
		} else {
			console.log('Setting attribute '+attribute+' to ',value);
			store.changing(item);
			item[attribute]=value;
		}
		//item[attribute]=value;
		store.onSet(item,attribute,old,value);
	}
	
});